Zepto(function($) {
  window.onload=function () {
    getAddressList();
  }
  /**
   *
   *
   *
   */
  $(".collect-money").on("click",".list",function () {
    var a = sessionStorage.getItem("comfir");
    if(a == "select"){
      sessionStorage.setItem("comfirmName",$(this)[0].getAttribute("data-name"));
      sessionStorage.setItem("comfirmMobile",$(this)[0].getAttribute("data-mobile"));
      sessionStorage.setItem("comfirmRegion",$(this)[0].getAttribute("data-region"));
      sessionStorage.setItem("comfirmDetailed",$(this)[0].getAttribute("data-detailed"));
      sessionStorage.setItem("comfirmAddressid",$(this)[0].getAttribute("data-addressid"));
      window.history.go(-1);
    }
  });
  $("#addresslist").on("click",".delette",function () {
    var addressid = $(this)[0].getAttribute("data-addressid");
    var btnArray = ['删除', '取消'];
    mui.confirm('', '确定删除该条记录吗？', btnArray, function(e) {
      if(e.index == 0) {
        deleteAddress(addressid);
      }
    });
  });
  function deleteAddress(addressId) {
    var data = {
      "addressId":addressId
    }
    $.ajax({
      type: 'POST',
      url: getUrl + '/wxmall/member/address/delete',
      data: data,
      dataType: 'json',
      timeout: 15000,
      headers: headers("memberAddress.delete"),
      // contentType:'application/json',
      success: function(data){
        callbackss(data);
      },
      error: function(xhr, type){
      }
    });
  }
  function callbackss(data) {
      if(data.success){
        getAddressList();
        mui.toast("删除地址成功！");
      }else{
        mui.toast("删除地址失败！");
      }
  }
  $("#addresslist").on("click",".checkbox-cell",function () {
    var addressid = $(this)[0].getAttribute("data-addressid");
    setDefaultAddressUsed(addressid);
  });
  /**
   * 设置默认地址
   *
   * @param count
   * @param specificationId
   */
  function setDefaultAddressUsed(addressid) {
    $.ajax({
      type: 'POST',
      url: getUrl + '/wxmall/member/address/setDefaultAddressUsed',
      data: {
        addressId:addressid
      },
      dataType: 'json',
      timeout: 15000,
      headers: headers("memberAddress.setDefaultAddressUsed"),
      context: $('body'),
      success: function(data){
      },
      error: function(xhr, type){

      }
    });
  }
  /**
   * 地址编辑
   */
  $("#addresslist").on("click",".address-edit",function () {
    sessionStorage.setItem("editAddressName",$(this)[0].getAttribute("data-name"));
    sessionStorage.setItem("editAddressMobile",$(this)[0].getAttribute("data-mobile"));
    sessionStorage.setItem("editAddressRegion",$(this)[0].getAttribute("data-region"));
    sessionStorage.setItem("editAddressDetailed",$(this)[0].getAttribute("data-detailed"));
    sessionStorage.setItem("editAddressAddressid",$(this)[0].getAttribute("data-addressid"));
    window.location.href = "/usercenter/addressmanage/editaddress.html";
  })
  //获取大家都在卖列表
  function getAddressList() {
    $.ajax({
      type: 'GET',
      url: getUrl + '/wxmall/member/address/list',
      data: {},
      dataType: 'json',
      timeout: 15000,
      headers: headers("memberAddress.list"),
      context: $('body'),
      success: function(data){
        getAddressListInit(data);
      },
      error: function(xhr, type){
      }
    })
  }
  function getAddressListInit(data) {
    $("#addresslist").html("");
    if(!data.success){
      return
    }
    var data1 = data.data;
    if(data1 != null && data1 != undefined && data1 != ""){
      var html = "";
      for(var i = 0; i < data1.length; i++){
        var name = "";
        if(data1[i].name != null){
          name = data1[i].name;
        }
        var mobile = "";
        if(data1[i].mobile != null){
          mobile = data1[i].mobile;
        }
        //region  detailed
        var region = "";
        if(data1[i].region != null){
          region = data1[i].region;
        }
        var detailed = "";
        if(data1[i].detailed != null){
          detailed = data1[i].detailed;
        }
        //addressid
        var used = "";
        if(data1[i].used != null){
          if(data1[i].used === "1"){
            used = 'checked';
          }
        }
        var addressid = "";
        if(data1[i].addressid != null){
          addressid = data1[i].addressid;
        }
        html += '<div data-addressid="'+addressid+'" data-name="'+name+'" data-mobile="'+mobile+'" data-region="'+region+'" data-detailed="'+detailed+'"  class="list">'+
        '<div class="list-up">'+
        '<div class="s-link">'+
        '<div class="link-name">'+ name +'</div>'+
        '<div class="link-phone">'+ mobile +'</div>'+
        '</div>'+
        '<div class="link-address">'+
        '<div class="address-v">'+ region + detailed + '</div>'+
        '<div data-addressid="'+addressid+'" data-name="'+name+'" data-mobile="'+mobile+'" data-region="'+region+'" data-detailed="'+detailed+'" class="address-edit">编辑</div>'+
        '</div>'+
        '</div>'+
        '<div class="list-down">'+
        '<div data-addressid="'+addressid+'" class="checkbox-cell">'+
        '<input type="checkbox" '+ used +'>'+
        '<label>设为默认</label>'+
        '</div>'+
        '<div data-addressid="'+addressid+'" class="delette">'+
        '<div class="delet-img"></div>'+
        '<div class="delet-n">删除</div>'+
        '</div>'+
        '</div>'+
        '</div>';
      }
      $("#addresslist").append(html);
    }
  }
  $(".cart-nogoods").on("click",".nogoods-find-btn",function () {
    window.location.href = "/index/index.html";
  })
  $(".nav-header").on("click",".nav-edit",function () {
    window.history.go(-1);
  });
  // 设为默认
  $("ul").on("click",".checkbox-cell",function () {
    var array = $("ul").find("input");
    array.forEach(function (item,index,array) {
      item.checked = false;
    });
    $(this).find("input")[0].checked = true;
    // $(this).css("color","#00c4d2");
  });
  //新增地址
  $(".form-style").on("click",".sub-bottom",function () {
    window.location.href = "/usercenter/addressmanage/createaddress.html";
  });
});